package com.hmkj.crm.billing.service.relevance.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmkj.crm.billing.domain.order.Order;
import com.hmkj.crm.billing.domain.order.PurchaseOrder;
import com.hmkj.crm.billing.domain.order.ReturnOrder;
import com.hmkj.crm.billing.domain.order.SalesOrder;
import com.hmkj.crm.billing.domain.relevance.ProductInventoryDetail;
import com.hmkj.crm.billing.dto.ProductInventoryDetailDTO;
import com.hmkj.crm.billing.enums.InventoryTypeEnum;
import com.hmkj.crm.billing.mapper.relevance.ProductInventoryDetailMapper;
import com.hmkj.crm.billing.service.order.PurchaseOrderService;
import com.hmkj.crm.billing.service.order.ReturnOrderService;
import com.hmkj.crm.billing.service.order.SalesOrderService;
import com.hmkj.crm.billing.service.relevance.ProductInventoryDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 产品库存关联表明细 服务shix
 *
 * @author sks.lwei
 * @date 2020/02/15
 */
@Service
public class ProductInventoryDetailServiceImpl extends ServiceImpl<ProductInventoryDetailMapper, ProductInventoryDetail> implements ProductInventoryDetailService {

    /**
     * 订单服务
     */
    @Resource
    private PurchaseOrderService purchaseOrderService;

    /**
     * 销售订单服务
     */
    @Resource
    private SalesOrderService salesOrderService;

    /**
     * 退货订单服务
     */
    @Resource
    private ReturnOrderService returnOrderService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDetail(String relProductInventoryUuid, Order order) {
        ProductInventoryDetail detail = new ProductInventoryDetail();
        detail.setUuid("pid_"+ IdWorker.get32UUID());
        detail.setInventoryUuid(relProductInventoryUuid);
        InventoryTypeEnum orderInventoryType = order.getOrderInventoryType();
        Integer number = order.getNumber();
        if(orderInventoryType.getType() < 0){
            number = -number;
        }
        detail.setNumber(number);
        detail.setOrderUuid(order.getOrderUuid());
        detail.setTotalPrice(order.getTotalPrice());
        detail.setInventoryType(orderInventoryType);
        this.save(detail);
    }

    @Override
    public IPage<ProductInventoryDetailDTO> listProductInventoryDetailPage(Page<ProductInventoryDetail> page, Wrapper<ProductInventoryDetail> wrapper) {
        Page<ProductInventoryDetail> inventoryDetailPage = this.page(page, wrapper);
        return inventoryDetailPage.convert(inventoryDetail -> {
            ProductInventoryDetailDTO dto = new ProductInventoryDetailDTO(inventoryDetail);

            InventoryTypeEnum inventoryType = inventoryDetail.getInventoryType();
            String orderUuid = inventoryDetail.getOrderUuid();

            switch (inventoryType){
                case INCOME:
                    //进货订单
                    PurchaseOrder purchaseOrder = purchaseOrderService.getPurchaseOrderByUuid(orderUuid);
                    BeanUtil.copyProperties(purchaseOrder, dto);
                    break;
                case SPENDING:
                    //销售订单
                    SalesOrder salesOrder = salesOrderService.getSalesOrderByUuid(orderUuid);
//                    BeanUtil.copyProperties(salesOrder, dto);
                    dto = dto.toSalesOrder(salesOrder);
//                    dto.setNumber(inventoryDetail.getNumber());
                    break;
                case RETURN:
                    //退货订单
                    ReturnOrder returnOrder = returnOrderService.getReturnOrderByUuid(orderUuid);
                    dto = dto.toReturnOrder(returnOrder);
                    break;
                default:
            }
            return dto;
        });
    }
}
